<?php

use app\models\Api;
use app\models\Isp;
use app\models\Package;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
use app\models\Agent;
use app\models\User;
use app\models\KeyMap;
use app\models\Cardtype;
use dosamigos\datepicker\DatePicker;

/**
 * @var $this yii\web\View
 * @var $card app\models\Card
 */

$this->title = '添加/修改绑定';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);?>

    <fieldset>
        <?php echo Html::activeHiddenInput($bind, 'id');?>
        <?php echo $form->field($bind, 'iccid')->textInput(['autocomplete' => 'off']);?>
        <?php echo $form->field($bind, 'imei')->textInput(['autocomplete' => 'off']);?>
        <?php //echo $form->field($model, 'username')->hint('请填写需要分配给的用户名。');?>
        <?php /*echo $form->field($model, 'id')->hiddenInput();*/?>
        <?php Pjax::begin(['id' => 'pjax_user']);?>
        <?php
        $username = Yii::$app->request->get('username');
        $userType='agent';
        if (!empty($username)) {
            if ($userType == 'agent') {
            /** @var Agent[] $agentList */
            $agentList = Agent::find()
                ->andWhere(['like', 'username', $username])
                ->andWhere(['status' => Agent::STATUS_OK])
                ->all();
            if (!empty($agentList)) {?>
                <div class="form-group">
                    <label class="control-label" for="orderForm-iccid_list">搜索到的代理商</label>
                    <div class="help-block">
                        <?php foreach ($agentList as $agent) {?>
                            <button type="button" class="btn btn-info btn-sm" onclick="$('[name=\'BindImei[username]\']').val('<?php echo $agent->username;?>');"><?php echo Html::encode($agent->username);?></button>
                        <?php }?>
                    </div>
                </div>
            <?php } else {?>
                <div class="form-group">
                    <div class="help-block">没有找到代理商：<?php echo Html::encode($username);?></button></div>
                </div>
            <?php }
        }
    }?>
    <?php Pjax::end();?>
</fieldset>

<div class="form-group">
    <div class="col-lg-offset-1 col-lg-11">
        <button type="button" class="btn btn-default" onclick="window.history.go(-1);"><i class="ace-icon fa fa-arrow-left bigger-110"></i>返回</button>
        <button class="btn btn-primary"><i class="ace-icon fa fa-check bigger-110"></i>保存</button>
        <button type="reset" class="btn btn-warning"><i class="ace-icon fa fa-undo bigger-110"></i>重置</button>
    </div>
</div>
<?php $form->end();?>
<script>
    window.onload=function (){

    }

    function pageInit() {

        $('[name="BindImei[username]"]').keyup(function () {
        //$("#OrderForm-username").keyup(function () {
            var username = $(this).val();
            $.pjax.reload('#pjax_user', {
                history: false,
                push: false,
                url: '<?php echo Url::current(['ajax' => 1]);?>&user_type=agent&username=' + encodeURIComponent(username)
            });
        });


    }

</script>
